package com.ygj.attendance.action;

import static com.base.common.GlobaConstants.ACTION_RESULT_DATA;
import static com.base.common.GlobaConstants.ASYNCHRONOUS;
import static com.base.common.GlobaConstants.CONTENT_TYPE;
import static com.base.common.GlobaConstants.CONTENT_TYPE_JSON;
import static com.base.common.GlobaConstants.JSON;
import static com.base.common.GlobaConstants.ROOT;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.dispatcher.StrutsRequestWrapper;

import com.base.common.ConfigConstants;
import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.base.common.persistent.model.AttendanceRecord;
import com.base.common.persistent.model.Employee;
import com.base.common.persistent.model.SignPoint;
import com.cjy.common.bean.Files;
import com.cjy.common.bean.OpAttributes;
import com.ygj.attendance.service.AttendanceService;

/**
 * 考勤action
 * 
 * @author Kat Colin 创建于 2015年12月14日下午6:00:26
 * 
 */

@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT_DATA, CONTENT_TYPE, CONTENT_TYPE_JSON }) })
public class AttendanceAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;

	@Resource
	private AttendanceService attendanceService;

	// 签到、签退参数
	private int categories;
	private int type;
	private int signPointId;
	private String address;
	private String description;

	// 考勤记录查询参数
	private String queryTime;
	private int compoundsId;

	/* Struts2文件上传的三个属性 */
	private File[] filePhotos;
	private String[] filePhotosFileName;
	private String[] filePhotosContentType;

	public int getCategories() {
		return categories;
	}

	public void setCategories(int categories) {
		this.categories = categories;
	}

	public String getQueryTime() {
		return queryTime;
	}

	public void setQueryTime(String queryTime) {
		this.queryTime = queryTime;
	}

	public int getSignPointId() {
		return signPointId;
	}

	public void setSignPointId(int signPointId) {
		this.signPointId = signPointId;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public int getCompoundsId() {
		return compoundsId;
	}

	public void setCompoundsId(int compoundsId) {
		this.compoundsId = compoundsId;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public File[] getFilePhotos() {
		return filePhotos;
	}

	public void setFilePhotos(File[] filePhotos) {
		this.filePhotos = filePhotos;
	}

	public String[] getFilePhotosFileName() {
		return filePhotosFileName;
	}

	public void setFilePhotosFileName(String[] filePhotosFileName) {
		this.filePhotosFileName = filePhotosFileName;
	}

	public String[] getFilePhotosContentType() {
		return filePhotosContentType;
	}

	public void setFilePhotosContentType(String[] filePhotosContentType) {
		this.filePhotosContentType = filePhotosContentType;
	}

	/**
	 * 考勤
	 * 
	 * @return
	 */

	@Action(value = "attendance", interceptorRefs = { @InterceptorRef("fileUploadInterceptor") })
	public String attendance() {
		AttendanceRecord attendanceRecord = new AttendanceRecord();
		if(type == 0){
			SignPoint signPoint = new SignPoint();
			signPoint.setId(signPointId);
			attendanceRecord.setSignPoint(signPoint);
		} else if(type == 1) {
			attendanceRecord.setDescription(description);
		}
		attendanceRecord.setAddress(address);
		attendanceRecord.setEmployee(new Employee()
				.setId(((List<Employee>) request.getSession().getAttribute(
						"users")).get(0).getId()));
		attendanceRecord.setCategories(categories);
		attendanceRecord.setSignTime(new Date());

		Files files = new Files();
		files.setFile(filePhotos);
		files.setFileContentType(filePhotosContentType);
		files.setFileFileName(filePhotosFileName);

		// 因为是上传文件，用了common-upload的拦截器处理之后request会报空，要得到一般请求的request头，就要对request做个转型，才能调用相应方法
		HttpServletRequest servletRequest = new StrutsRequestWrapper(request);
		String basePath = null;
		String path = servletRequest.getContextPath();
		basePath = servletRequest.getScheme() + "://"
				+ ConfigConstants.SERVERIP + ":"
				+ servletRequest.getServerPort() + path + "/";
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", attendanceService.attendance(attendanceRecord,
							basePath, files, type, compoundsId)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	@Action(value = "queryAttendanceRecordByMonth")
	public String queryAttendanceRecordByMonth() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 1).add(
					"result",
					attendanceService.queryAttendanceRecordByMonth(
							((List<Employee>) request.getSession()
									.getAttribute("users")).get(0).getId(),
							compoundsId, queryTime)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	@Action(value = "manageAttendanceRecord")
	public String manageAttendanceRecord(){
        try {
            actionResult = ActionResult.getActionResult(new OpAttributes().add(
                    "code", 1).add("result", attendanceService.manageQueryAttendanceRecord(compoundsId, queryTime)));
        } catch (Exception e) {
            e.printStackTrace();
            actionResult = ActionResult.getActionResult(new OpAttributes().add(
                    "code", 0));
        }
        return returnType();
	}
}
